{% extends 'base_wide_page.html' %}

{% load seahub_tags avatar_tags i18n staticfiles %}


{% block sub_title %}{% trans "History" %} - {% endblock %}

{% block extra_style %}
<style type="text/css">
    .go-back { top:-3px; }
</style>
{% endblock %}

{% block wide_page_content %}
<h2>{% blocktrans %}<span class="op-target">{{ u_filename }}</span> Version History{% endblocktrans %}</h2>

{% if referer %}
<a href="#" class="go-back" title="{% trans "Back" %}">
    <span class="icon-chevron-left"></span>
</a>
{% endif %}

<p class="tip">{% trans "Tip: a new version will be generated after each modification, and you can restore the file to a previous version." %}</p>
        <div class="commit-list-topbar ovhd">
            <p class="path fleft">
                {% trans 'Current Path:' %}
                {% for name, link in zipped %}
                {% if not forloop.last %}
                <a href="{{ SITE_ROOT }}#common/lib/{{ repo.id }}/{{ link|strip_slash }}">{{ name }}</a> /
                {% else %}
                <a href="{% url 'view_lib_file' repo.id path %}" target="_blank" >{{ name }}</a>
                {% endif %}
                {% endfor %}
            </p>
        </div>
        <table class="commit-list">
            <thead>
                <tr>
                    <th width="25%" class="time">{% trans 'Time' %}</th>
                    <th width="25%">{% trans 'Modifier' %}</th>
                    <th width="20%">{% trans 'Size' %}</th>
                    <th width="30%">{% trans 'Operations' %}</th>
                </tr>
            </thead>

            <tbody>
            </tbody>
        </table>

<div id="history-more">
    <div id="history-more-loading"><span class="loading-icon loading-tip"></span></div>
    <button id="history-more-btn" class="full-width-btn hide">{% trans 'More' %}</button>
</div>

{% endblock %}

{% block extra_script %}

<script type="text/javascript" src="{% static "scripts/lib/underscore.js" %}"></script>
<script type="text/javascript" src="{% static "scripts/lib/moment-with-locales.min.js" %}"></script>

<script type="text/template" id="file-history-tr-tmpl">
<tr>
    <td class="time">
        <span title="<%- time %>"><%- time_from_now %></span>
        <% if (is_first_commit) { %>
        {% trans '(current version)' %}
        <% } %>
        <% if (rev_renamed_old_path) { %>
        <br /><span class="tip"><%- renamed_or_moved_tip %></span>
        <% } %>
    </td>
    <td>
        <% if (creator_name) { %>
        <img src="<%- creator_avatar_url %>" width="16" height="16" class="avatar" />
        <a class="vam" title="<%- creator_name %>" href="<%- creator_profile_url %>"><%- creator_name %></a>
        <% } else { %>
        {% trans 'Unknown' %}
        <% } %>
    </td>
    <td><%- size %></td>
    <td>
        {% if can_revert_file %}
        <% if (!is_first_commit) { %>
        <a href="#" class="op vh restore-file" data-commit_id="<%- commit_id %>" data-commit_path="<%- path %>">{% trans 'Restore' %}</a>
        <% } %>
        {% endif %}
        <a href="<%- download_file_url %>" class="op vh">{% trans 'Download' %}</a>
        <a href="<%- view_history_file_url %>" class="op vh" target="_blank">{% trans 'View' %}</a>
        {% if can_compare %}
        <a href="<%- diff_url %>" class="op vh text-diff">{% trans 'Diff' %}</a>
        {% endif %}
    </td>
</tr>
</script>


<script type="text/javascript">

var setLocale = function() {
    var lang_code = '{{LANGUAGE_CODE}}';
    var m_lang_code;
    if (lang_code == 'en') {
        m_lang_code = 'en-gb';
    } else if (lang_code == 'es-ar' || lang_code == 'es-mx') {
        m_lang_code = 'es';
    } else {
        m_lang_code = lang_code;
    }
    moment.locale(m_lang_code);
};

var util_getRelativeTimeStr = function(m) {
    var now = new Date();
    if (m - now > 0) {
        return "{% trans "Just now" %}";
    } else {
        return m.fromNow();
    }
};

setLocale();

var file_path = '{{path|escapejs}}',
    next_start_commit, rev_renamed_old_path,
    is_first_commit = true,
    $tbodyContainer = $('tbody'),
    $history_more_btn = $('#history-more-btn'),
    $history_more_loading = $('#history-more-loading'),
    tmpl = _.template($('#file-history-tr-tmpl').html());

var get_file_revisions = function(path, commit_id) {

    $history_more_btn.addClass('hide');
    $history_more_loading.removeClass('hide');

    var data = {
        'path': path
    }; // for the first request

    if (commit_id != undefined) {
        $.extend(data, {'commit_id': commit_id});
    }

    $.ajax({
        url:'{% url "api-v2.1-file-history-view" repo.id %}',
        data: data,
        cache: false,
        dataType: 'json',
        success: function(resp) {
            next_start_commit = resp.next_start_commit;
            $history_more_loading.addClass('hide');

            if (resp.data.length) {
                $(resp.data).each(function(index, item) {
                    if (item.rev_renamed_old_path) {
                        rev_renamed_old_path = item.rev_renamed_old_path;
                    }

                    var creator_profile_url = '{{SITE_ROOT}}profile/' + encodeURIComponent(item.creator_email);
                    var download_file_url = '{{SITE_ROOT}}repo/{{repo.id}}/' + item.rev_file_id + '/download/?p=' + encodeURIComponent(item.path);
                    var view_history_file_url = '{{SITE_ROOT}}repo/{{repo.id}}/history/files/?obj_id=' + item.rev_file_id + '&commit_id=' + item.commit_id + '&p=' + encodeURIComponent(item.path);
                    var diff_url = '{{SITE_ROOT}}repo/text_diff/{{repo.id}}/?commit=' + item.commit_id + '&p=' + encodeURIComponent(item.path) + '&referer=' + encodeURIComponent(location.href);
                    var m = moment(item.ctime);
                    var render_data = $.extend({}, item, {
                        'is_first_commit': is_first_commit,
                        'renamed_or_moved_tip': "{% trans '(Renamed or moved from {placeholder})' %}".replace('{placeholder}', item.rev_renamed_old_path),
                        'size': filesizeformat(item.size),
                        'time': m.format('LLLL'),
                        'time_from_now': util_getRelativeTimeStr(m),
                        'creator_profile_url': creator_profile_url,
                        'download_file_url': download_file_url,
                        'view_history_file_url': view_history_file_url,
                        'diff_url': diff_url
                    });

                    $tbodyContainer.append($(tmpl(render_data)));
                    is_first_commit = false;
                });
                if (next_start_commit) {
                    $history_more_btn.removeClass('hide');
                } else {
                    $history_more_btn.addClass('hide');
                }
            } else if (next_start_commit) {
                var path = rev_renamed_old_path ? rev_renamed_old_path : file_path;
                get_file_revisions(path, next_start_commit);
            }

        },
        error: function (xhr, textStatus, errorThrown) {
            $history_more_loading.addClass('hide');
            var error;
            if (xhr.responseText) {
                error = JSON.parse(xhr.responseText).error_msg;
            } else {
                error = "{% trans "Failed. Please check the network." %}";
            }
            feedback(error, 'error');
        }
    });
};

// get first batch of file revisions
// seahub web api will use repo's first head commit_id
// if no commit_id parameter passed
get_file_revisions(file_path);

$('#history-more-btn').on('click', function() {
    // get more batch of file revisions
    // next_start_commit = resp.next_start_commit;
    var path = rev_renamed_old_path ? rev_renamed_old_path : file_path;
    get_file_revisions(path, next_start_commit);
});

$('.commit-list').on('click', '.restore-file', function () {
    var _this = $(this),
        commit_id = _this.attr('data-commit_id'),
        path = _this.attr('data-commit_path');

    $.ajax({
        url: "{% url 'api-v2.1-file-view' repo.id %}" + "?p=" + encodeURIComponent(path),
        type: 'POST',
        data: {'operation': 'revert', 'commit_id': commit_id},
        cache: false,
        dataType: 'json',
        beforeSend: prepareCSRFToken,
        success: function() {
            var msg = "{% trans "Successfully restored {filename}" %}".replace('{filename}', "{{u_filename|escapejs}}");
            feedback(msg, 'success');
            $tbodyContainer.empty();
            is_first_commit = true;
            get_file_revisions(file_path);
        },
        error: ajaxErrorHandler
    });
    return false;
});

{% include 'snippets/go_back_js.html' %}
</script>
{% endblock %}
